package com.hxbd.test.utils;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.ITestResult;

import com.duan.test.utils.Excel.FileUtils;
import com.duan.test.utils.common.CommonUtils;
import com.hxbd.test.handles.PageElementHandler;
import com.hxbd.test.utils.webdriver.WebDriverUtil;

public class WebTestUtil extends PlatformUtil{
	protected static WebDriver      driver   = null;
    private static final Logger log = Logger.getLogger(WebTestUtil.class);
	private static String chromedriver = CommonUtils.getFilePath(PageElementHandler.getStoreSettingProperties().getProperty("testTools_add")) + "chromedriver.exe";
	private static String IEDriver = CommonUtils.getFilePath(PageElementHandler.getStoreSettingProperties().getProperty("testTools_add")) + "IEDriverServer.exe";
    
    public static WebDriver getDriver() {
		return driver;
	}
	/**
     * <p>Description: 初始化浏览器<／p>
     */
    protected void initSeleniumInstance(){
        if (PageElementHandler.getStoreSettingProperties().getProperty("TEST_BROWSER").equalsIgnoreCase("firefox"))
        {
        	driver = WebDriverUtil.getFireFoxDriver();
        }
        else if (PageElementHandler.getStoreSettingProperties().getProperty("TEST_BROWSER").equalsIgnoreCase("IE"))
        {
        	driver = WebDriverUtil.getIEDriver(IEDriver);
        }
        else if (PageElementHandler.getStoreSettingProperties().getProperty("TEST_BROWSER").equalsIgnoreCase("chrome")){
        	driver = WebDriverUtil.getChromeDriver(chromedriver);  
        }
        else {
        	log.error("fail:找不到改浏览器");
        	Assert.fail("fail:找不到改浏览器");
        }
    } 
    
    /**
     * 
     * <p>Description: 初始化浏览器：remote grid分发<／p>
     * @param browser 浏览器类型
     * @param remoteAdd 远程测试地址ip
     * @throws MalformedURLException
     */
    protected void initSeleniumInstance(String browser,String remoteAdd) throws MalformedURLException {
        if (browser.equalsIgnoreCase("firefox"))
        {
        	driver = WebDriverUtil.getRemoteFireFoxDriver(remoteAdd);
        }
        else if (browser.equalsIgnoreCase("IE"))
        {
        	driver = WebDriverUtil.getRemoteIEDriver(remoteAdd,chromedriver);
        }
        else if (browser.equalsIgnoreCase("chrome")){
        	driver = WebDriverUtil.getRemoteChromeDriver(remoteAdd);  
        }
        else {
        	log.error("fail:找不到改浏览器");
        	Assert.fail("fail:找不到改浏览器");
        }
    } 
    
	
	/**
	 * 自动截图，保存图片到本地以及html结果文件中
	 * @param tr 测试结果对象
	 * @return String 截图绝对路径
	 */
	public static String takeScreenShot(ITestResult tr) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
		String mDateTime = formatter.format(new Date());
		String filePath;
		String ip = "";
		try {
			ip = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		filePath = "\\\\"+ ip +"\\G7S_AutoTest\\testResult\\capturePic\\TestNG_OUT";
		String fileName2 = FileUtils.getParentFolder(CommonUtils.getFilePath(""))+"/surefire-reports/capturePic/"+mDateTime + "_" + getTestClassName(tr) + "_" + tr.getName()+".jpg";
//		String fileName = filePath + "\\" +mDateTime + "_" + getTestClassName(tr) + "_" + tr.getName()+".jpg";
		WebDriver driver = getDriver();
		File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
        try {
			org.apache.commons.io.FileUtils.copyFile(srcFile,new File(fileName2));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.error("Can't save screenshot");
			e.printStackTrace();
		}
/*		Reporter.setCurrentTestResult(tr);
		Reporter.log(fileName);
           //这里实现把图片链接直接输出到结果文件中，通过邮件发送结果则可以直接显示图片
		Reporter.log("<img src=\"" + fileName + "\"/>");*/
		return fileName2;
	}
}
